<html>
  <head>
    <title>Library</title>
    <link href="style.css" rel="stylesheet" type="text/css">
  </head>
  
  <body>

<?php

include_once('conf.php');
include_once('includes/functions.php');


if (isset($_GET['string']) && isset($_GET['book'])) {
  
  $path = hexToStr ($_GET['book']);
  $book_details = pathinfo($path);
  $file = $book_details['filename'];
  
  $search_string = str_replace(' ', '+', trim($_GET['string']));
  
  $results = fetch_book($_GET['type'],$search_string);
  
  if (is_array($results) && $results['result'] == 1) {

      cache_book($file, $results);
      
      echo 'Confirmed book, cached ISBN ' . $results["isbn"] 
            . 'and title ' . $results["title"] . ' by '
            . $results["author"] .'<br />';
    
      show_cover($results['isbn']);
        
      isbn_input($_GET['book']);

      echo '</div>';
    }
    elseif (is_array($results) && $results['result'] > 1) {
    
      foreach ($results as $book_result) {
      
        if (is_array($book_result)) {
      
          echo '<table style="width:500px;padding:0;margin:1em 0 0 0;border:0;border-collapse: collapse;" class="resultslist">
          <tr style="padding:0;margin:0;border:0" class="results_top">
              <td style="text-align:left;padding:0;margin:0;border:0">' . $book_result["isbn"]
              . '</td>
              <td style="text-align:right;">' . $book_result["author"]
              . '</td></tr>
              <tr style="padding:0;margin:0;border:0" class="results_bottom">
              <td style="text-align:left;padding:0;margin:0;border:0">' . $book_result["title"] . '</td>
              <td style="text-align:right;"><a href="book.php?book=' . $_GET["book"]
              . '&string=' . $book_result["isbn"] . '&type=isbn">Select</a></td></tr>
              </table>';
        }
      }
    }
  else {
    echo 'Still no results, try again <br />Filename:' . $file . '<br /><br />';
    isbn_input($_GET['book']);
  }
}
  
elseif (isset ($_GET['book'])) {

  $path = hexToStr ($_GET['book']);
  $book_details = pathinfo($path);
  $file = $book_details['filename'];

  
  if (strpos($file,'(html)')) {
    $book_filename = substr($file,0,-6);
  }
  else {
    $book_filename = $file;
  }
  
  $cached_book = cache_check($book_filename);

  if ($cached_book) {
    
    show_cover($cached_book[0]);
    
    echo 'Confirmed book, ISBN ' . $cached_book["isbn"] 
            . ' and title ' . $cached_book["title"] . ' by '
            . $cached_book["author"] .'<br />';
    
    isbn_input($_GET['book']);
    
  }
  else {
    $results = scrape_book($file);
    
    if (is_array($results) && $results['result'] == 1) {

      cache_book($file, $results);
      
      echo 'Confirmed book, cached ISBN ' . $results["isbn"] 
            . ' and title ' . $results["title"] . ' by '
            . $results["author"] .'<br />';
    
      show_cover($results['isbn']);
        
      isbn_input($_GET['book']);

      echo '</div>';
    }
    elseif (is_array($results) && $results['result'] > 1) {
    
      foreach ($results as $book_result) {
      
        if (is_array($book_result)) {
      
          echo '<table style="width:500px;padding:0;margin:1em 0 0 0;border:0;border-collapse: collapse;" class="resultslist">
          <tr style="padding:0;margin:0;border:0" class="results_top">
              <td style="text-align:left;padding:0;margin:0;border:0">' . $book_result["isbn"]
              . '</td>
              <td style="text-align:right;">' . $book_result["author"]
              . '</td></tr>
              <tr style="padding:0;margin:0;border:0" class="results_bottom">
              <td style="text-align:left;padding:0;margin:0;border:0">' . $book_result["title"] . '</td>
              <td style="text-align:right;"><a href="book.php?book=' . $_GET["book"]
              . '&string=' . $book_result["isbn"] . '&type=isbn">Select</a></td></tr>
              </table>';
        }
      }
    }
    else {
    
      isbn_input($_GET["book"]);
    }
  }
}


function scrape_book($file) {

  // $book_details = pathinfo($file);

  if (strpos($file,'(html)')) {
    $book_filename = substr($file,0,-6);
  }
  else {
    $book_filename = $file;
  }

  $book = explode(' - ',$book_filename);

  if (count($book) == 4) {
    $author = trim($book[3]);
    $title = trim($book[2]);
  }
  elseif (count($book) == 3) {
    $author = trim($book[0]);
    $title = trim($book[2]);
  }
  elseif (count($book) == 2) {
    $author = trim($book[0]);
    $title = trim($book[1]);
  }
  else {
    echo 'Unknown pattern<br /><br /><pre>
      $book';
    print_r ($book);
    echo '
      $book_details
      ';
    print_r ($book_details);
  echo '</pre><br /><br />';
  }
  echo 'Scraped:<br>
            Author: ' . $author . '<br />
            Title: ' . $title . '<br />';
  
  if (variable_get('isbndb_key','') !== '') {
    
    $searchstring = str_replace(' ','+',$title) . '+by+' . str_replace(' ','+',$author);
    $book_details = fetch_book('combined',$searchstring);
    
    if ($book_details['result'] !== 0) {
      // array_dump($book_details, 'Scraped');//debug
      return $book_details;
    }
    else {
      echo '<br />Cannot locate book by title.<br />Filename: ' . $book_filename . '<br /><br />';
    }
  }
}

function show_cover($isbn) {

  echo '<img src="http://covers.librarything.com/devkey/' . variable_get('lt_key') . '/large/isbn/' . $isbn . '" />';
  
  }

function isbn_input($book) {
    echo '<div = "searcharea">Wrong book or no result? Please provide more search details.<br /><br />';
    echo '<form name="book_isbn" action="book.php" method="get">
            <input type="text" name="string" />
            <input type="radio" name="type" value="isbn" checked>ISBN 
            <input type="radio" name="type" value="combined">Fulltext 
            <input type="hidden" name="book" value="' . $book . '" />
            <input type="submit" value="Submit" />
          </form>';
    echo '</div>';
  }
